﻿using System;
using System.Windows;
using System.Windows.Controls;

namespace Elca.MvvmHelpers {

    public class ScrollHelper {

        public static readonly DependencyProperty ScrollToItemProperty =
                DependencyProperty.RegisterAttached(
                        "ScrollToItem", typeof (object), typeof (ScrollHelper),
                        new UIPropertyMetadata(OnScrollToItemPropertyChanged));


        public static object GetScrollToItem(ListBox lb) {
            return lb.GetValue(ScrollToItemProperty);
        }

        public static void SetScrollToItem(ListBox lb, bool value) {
            lb.SetValue(ScrollToItemProperty, value);
        }

        private static void OnScrollToItemPropertyChanged(DependencyObject d,
                                                          DependencyPropertyChangedEventArgs e) {
            var lb = (ListBox) d;

            lb.ScrollIntoView(e.NewValue);
        }
    }
}
